package com.zhengke.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

  

@Controller
public class ViewPicAction {
	private static final long serialVersionUID = 1L;
	@RequestMapping("view.do")
	public void view(HttpServletRequest request, HttpServletResponse response) throws IOException {
			
		String path = request.getParameter("path");
		if(path==null||"".equals(path)){
			return;
		}
		
		FileInputStream is = new FileInputStream(path);
		int i = is.available(); // 得到文件大小
		byte data[] = new byte[i];
		is.read(data); // 读数据
		is.close();
		response.setContentType("image/*"); // 设置返回的文件类型		OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
		toClient.write(data); // 输出数据
		toClient.flush();
		toClient.close();

	}
	
	/**
	 * 查看相对路径图片
	 * @throws IOException
	 */
	@RequestMapping("viewRelative.do")
	public void viewRelative(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String path = 	request.getParameter("path");
		if(path==null||"".equals(path)){
			return;
		}
		String name = path.substring(path.lastIndexOf("/"));
		
		String realpath = request.getSession().getServletContext().getRealPath(path.substring(0,path.lastIndexOf("/")));
		path= realpath+name;
		// path=new String(path.getBytes("iso-8859-1"),"UTF-8"); 
		
		FileInputStream is = new FileInputStream(path);
		int i = is.available(); // 得到文件大小
		byte data[] = new byte[i];
		is.read(data); // 读数据

		is.close();
		response.setContentType("image/*"); // 设置返回的文件类型

		OutputStream toClient = response.getOutputStream(); // 得到向客户端输出二进制数据的对象
		toClient.write(data); // 输出数据
		toClient.flush();
		toClient.close();
	}
	
	
}
